package com.example.demo.dubbo.impl;

import com.example.demo.dubbo.OrderDubboService;
import com.example.demo.po.OperationResult;
import com.example.demo.po.OperatorInfo;
import com.example.demo.po.Order;
import com.example.demo.po.OrderDetail;
import com.example.demo.service.OrderService;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class OrderDubboServiceImpl implements OrderDubboService {

    @Resource
    private OrderService orderService;

    @Override
    public Order getById(Long id) {
        return orderService.getById(id);
    }

    @Override
    public List<Order> getByUserId(Long userId) {
        return orderService.getByUserId(userId);
    }

    @Override
    public List<Order> getAll() {
        return orderService.getAll();
    }

    @Override
    public boolean create(Order order) {
        return orderService.create(order);
    }

    @Override
    public boolean update(Order order) {
        return orderService.update(order);
    }

    @Override
    public boolean deleteById(Long id) {
        return orderService.deleteById(id);
    }

    @Override
    public List<Order> queryOrders(Long userId, String status, LocalDateTime startTime, LocalDateTime endTime) {
        // 示例实现：调用 orderService.getAll() 并简单过滤
        List<Order> all = orderService.getAll();
        List<Order> result = new java.util.ArrayList<>();
        for (Order o : all) {
            boolean match = (userId == null || userId.equals(o.getUserId()))
                    && (status == null || status.equals(o.getStatus()))
                    && (startTime == null || (o.getCreateTime() != null && o.getCreateTime().isAfter(startTime)))
                    && (endTime == null || (o.getCreateTime() != null && o.getCreateTime().isBefore(endTime)));
            if (match)
                result.add(o);
        }
        return result;
    }

    @Override
    public OperationResult operateOrder(Order order, OperatorInfo operatorInfo) {
        // 示例实现：根据操作人和订单简单返回结果
        OperationResult result = new OperationResult();
        if (order == null || operatorInfo == null) {
            result.setSuccess(false);
            result.setMessage("参数不能为空");
            return result;
        }
        // 假设操作成功
        result.setSuccess(true);
        result.setMessage("操作成功 by " + operatorInfo.getName());
        Map<String, Object> data = new HashMap<>();
        data.put("orderId", order.getId());
        data.put("operator", operatorInfo.getName());
        result.setData(data);
        return result;
    }

    @Override
    public Map<Long, OrderDetail> batchGetOrderDetails(List<Long> orderIds, Map<String, Object> extraParams) {
        // 示例实现：返回每个订单ID对应的OrderDetail
        Map<Long, OrderDetail> map = new HashMap<>();
        if (orderIds != null) {
            for (Long id : orderIds) {
                Order order = orderService.getById(id);
                if (order != null) {
                    OrderDetail detail = new OrderDetail();
                    detail.setOrderId(order.getId());
                    detail.setOrderNo(order.getOrderNo());
                    detail.setStatus(order.getStatus());
                    detail.setExtraInfo(extraParams);
                    map.put(id, detail);
                }
            }
        }
        return map;
    }
}